/*
 * @lc app=leetcode.cn id=56 lang=cpp
 *
 * [56] 合并区间
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;
// @lc code=start
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        int n = intervals.size();
        vector<vector<int>> res;
        sort(intervals.begin(),intervals.end());
        for(int i = 0;i < n;i ++) {
            int  l = intervals[i][0],r = intervals[i][1];
            if(!res.size() || res.back()[1] < l) {
                res.push_back({l,r});
            }else {
                res.back()[1] = max(r,res.back()[1]);
            }
        }
        return res;
    }
};
// @lc code=end

